// ChSMessageParser.cpp : implementation file
//

#include "stdafx.h"
#include "ChatServer.h"
#include "ChSMessageParserDlg.h"


// CChSMessageParserDlg dialog

IMPLEMENT_DYNAMIC(CChSMessageParserDlg, CDialog)

CChSMessageParserDlg::CChSMessageParserDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CChSMessageParserDlg::IDD, pParent)
	, m_uMessagesCount(0)
{

}

CChSMessageParserDlg::~CChSMessageParserDlg()
{
	m_lbMessages.Detach();
}

void CChSMessageParserDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_USER_NAMES_FROM, m_cmbUserNamesFrom);
	DDX_Control(pDX, IDC_COMBO_USER_NAMES_TO, m_cmbUserNamesTo);
	DDX_Text(pDX, IDC_EDIT_MESSAGES_COUNT, m_uMessagesCount);
}

void CChSMessageParserDlg::BeginParsing()
{
	CChSMessageParserDlg dlg;
	dlg.FileName(CChSLogWriter::FileName());
	dlg.DoModal();
}

void CChSMessageParserDlg::OnCancel()
{
	try
	{
		if(IDYES == AfxMessageBox(_T("Are you wan save current result to file?"), MB_YESNO))
		{
			CFileDialog fDlg(FALSE,NULL,NULL,4|2,_T("Text (*.txt)|*.txt|Log (*.log)|*.log|All Files (*.*)|*.*||"));
			if(IDOK == fDlg.DoModal())
			{
				SaveFile(fDlg.GetPathName());
			}
		}
	}
	catch (CMemoryException* e)
	{
		e->ReportError();
	}
	catch (CFileException* e)
	{
		e->ReportError();
	}
	catch (CException* e)
	{
		e->ReportError();
	}
	
	CDialog::OnCancel();
}

void CChSMessageParserDlg::ConstructUsersListFrom(const CString& strMessage)
{	
	CString strUserName = GetFrom(strMessage);
	if(0 > m_cmbUserNamesFrom.FindString(-1,strUserName))
		m_cmbUserNamesFrom.AddString(strUserName);
}

void CChSMessageParserDlg::ConstructUsersListTo(const CString& strMessage)
{	
	CString strUserName = GetTo(strMessage);
	if(0 > m_cmbUserNamesTo.FindString(-1,strUserName))
		m_cmbUserNamesTo.AddString(strUserName);
}

CString CChSMessageParserDlg::GetTo(const CString& strMessage)
{
	CString strUserName = strMessage;
	int nPosBegin = strUserName.Find(_T("]"));

	int nPos = strUserName.Find(_T(":"),nPosBegin);

	if(nPos>0)
		strUserName.Truncate(nPos);
	nPos = strUserName.Find(_T("to"));

	if(nPos>0)
	{
		strUserName = strUserName.Right(strUserName.GetLength() - nPos - 3);
		strUserName.TrimLeft();
		strUserName.TrimRight();
		return strUserName;
	}

	return _T("");
}

CString CChSMessageParserDlg::GetFrom(const CString& strMessage)
{
	CString strUserName = strMessage;
	int nPosBegin = strUserName.Find(_T("]"));

	int nPos = strUserName.Find(_T(":"),nPosBegin);

	if(nPos>0)
		strUserName.Truncate(nPos);
	nPos = strUserName.Find(_T("to"));

	if(nPos>0)
		strUserName.Truncate(nPos);

	strUserName = strUserName.Right(strUserName.GetLength() - nPosBegin-1);
	strUserName.TrimLeft();
	strUserName.TrimRight();

	return strUserName;
}

void CChSMessageParserDlg::SaveFile(const CString& strFileName)
{
	try
	{
		CUTF16File file;
		file.Open(strFileName,CFile::modeCreate|CFile::modeWrite|CFile::typeText);
				
		POSITION pos = m_MsgArr.GetHeadPosition();
		CString strCurMsg;
		while(NULL != pos)
		{
			strCurMsg = m_MsgArr.GetNext(pos);
			if( ContainFrom(strCurMsg) && ContainTo(strCurMsg) )
			{
				file.WriteString(strCurMsg,TRUE);
				file.WriteString(_T("\r\n"),TRUE);
			}
		}	

		file.Close();
	}
	catch (CMemoryException* e)
	{
		e->ReportError();
	}
	catch (CFileException* e)
	{
		e->ReportError();
	}
	catch (CException* e)
	{
		e->ReportError();
	}
}

void CChSMessageParserDlg::LoadFile()
{
	try
	{
		CUTF16File file;
		file.Open(m_strFileName,CFile::modeRead|CFile::typeText);
		m_lbMessages.ResetContent();
		m_cmbUserNamesFrom.ResetContent();
		m_cmbUserNamesTo.ResetContent();
		m_strFilterFrom = _T("");
		m_strFilterTo = _T("");
		m_MsgArr.RemoveAll();

		m_cmbUserNamesFrom.AddString(_T(""));
		m_cmbUserNamesTo.AddString(_T(""));

		CString strTempString = _T("");
		while(TRUE == file.ReadString(strTempString))
		{
			m_MsgArr.AddTail(strTempString);
			m_lbMessages.AddString(strTempString);
			ConstructUsersListFrom(strTempString);	
			ConstructUsersListTo(strTempString);
		}

		m_uMessagesCount = m_lbMessages.GetCount();
		UpdateData(FALSE);
	}
	catch (CMemoryException* e)
	{
		e->ReportError();
	}
	catch (CFileException* e)
	{
		e->ReportError();
	}
	catch (CException* e)
	{
		e->ReportError();
	}
}

void CChSMessageParserDlg::ExecuteFilter()
{
	m_lbMessages.ResetContent();
	POSITION pos = m_MsgArr.GetHeadPosition();
	CString strCurMsg;
	while(NULL != pos)
	{
		strCurMsg = m_MsgArr.GetNext(pos);
		if( ContainFrom(strCurMsg) && ContainTo(strCurMsg) )
			m_lbMessages.AddString(strCurMsg);
	}

	m_uMessagesCount = m_lbMessages.GetCount();
	UpdateData(FALSE);
}

BOOL CChSMessageParserDlg::ContainFrom(const CString& strMessage)
{
	if(m_strFilterFrom == _T(""))
		return TRUE;

	return GetFrom(strMessage) == m_strFilterFrom;
}

BOOL CChSMessageParserDlg::ContainTo(const CString& strMessage)
{
	if(m_strFilterTo == _T(""))
		return TRUE;

	return GetTo(strMessage) == m_strFilterTo;
}

BOOL CChSMessageParserDlg::PreTranslateMessage(MSG* pMsg)
{
	switch( pMsg->message )
	{
	case WM_KEYDOWN:
		switch(pMsg->wParam)
		{
		case VK_DELETE:
			{
				int n = m_lbMessages.GetCurSel();
				if( n >= 0 )
				{
					CString strRemovedString;
					m_lbMessages.GetText(n,strRemovedString);
					m_lbMessages.DeleteString(n);

					m_uMessagesCount = m_lbMessages.GetCount();
					UpdateData(FALSE);

					POSITION pos = m_MsgArr.Find(strRemovedString);
					if( NULL != pos )
						m_MsgArr.RemoveAt(pos);
				}			
			}
			break;
		}
		break;
	}
	return CDialog::PreTranslateMessage(pMsg);
}


BEGIN_MESSAGE_MAP(CChSMessageParserDlg, CDialog)
	ON_COMMAND(ID_FILE_OPEN32777, &CChSMessageParserDlg::OnFileOpen32777)
	ON_CBN_SELCHANGE(IDC_COMBO_USER_NAMES_FROM, &CChSMessageParserDlg::OnCbnSelchangeComboUserNamesFrom)
	ON_CBN_SELCHANGE(IDC_COMBO_USER_NAMES_TO, &CChSMessageParserDlg::OnCbnSelchangeComboUserNamesTo)
	ON_COMMAND(ID_FILE_SAVE32778, &CChSMessageParserDlg::OnFileSave32778)
	ON_COMMAND(ID_FILE_EXIT32776, &CChSMessageParserDlg::OnFileExit32776)
END_MESSAGE_MAP()


BOOL CChSMessageParserDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	m_lbMessages.Attach(GetDlgItem(IDC_LIST_MESSAGES)->m_hWnd);
	LoadFile();

	return TRUE;
}
void CChSMessageParserDlg::OnFileOpen32777()
{
	CFileDialog fDlg(TRUE,NULL,NULL,4|2,_T("Text (*.txt)|*.txt|Log (*.log)|*.log|All Files (*.*)|*.*||"));
	if(IDOK == fDlg.DoModal())
	{
		FileName(fDlg.GetPathName());
		LoadFile();
	}
}

void CChSMessageParserDlg::OnCbnSelchangeComboUserNamesFrom()
{	
	m_cmbUserNamesFrom.GetLBText(m_cmbUserNamesFrom.GetCurSel(),m_strFilterFrom);
	ExecuteFilter();
}

void CChSMessageParserDlg::OnCbnSelchangeComboUserNamesTo()
{	
	m_cmbUserNamesTo.GetLBText(m_cmbUserNamesTo.GetCurSel(),m_strFilterTo);
	ExecuteFilter();
}

void CChSMessageParserDlg::OnFileSave32778()
{
	CFileDialog fDlg(FALSE,NULL,NULL,4|2,_T("Text (*.txt)|*.txt|Log (*.log)|*.log|All Files (*.*)|*.*||"));
	if(IDOK == fDlg.DoModal())
	{
		SaveFile(fDlg.GetPathName());
	}
}

void CChSMessageParserDlg::OnFileExit32776()
{
	OnCancel();
}
